package main.service;

import java.util.*;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import main.entity.*;
import main.vo.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

@Service
public class URLConnectionUserService {

    public static String GetResponse(String Info) throws IOException
    {
        String path = "http://222.24.192.216:8085/PublicRequest?RequestName=UserNoLogin2"; 
         
        //1, 得到URL对象 
        URL url = new URL(path); 
           
        //2, 打开连接 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
           
        //3, 设置提交类型 
        conn.setRequestMethod("POST"); 
           
        //4, 设置允许写出数据,默认是不允许 false 
        conn.setDoOutput(true); 
        conn.setDoInput(true);//当前的连接可以从服务器读取内容, 默认是true 
           
        //5, 获取向服务器写出数据的流 
        OutputStream os = conn.getOutputStream(); 
        //参数是键值队  , 不以"?"开始 
        os.write(Info.getBytes());
        
        
        //os.write("googleTokenKey=&username=admin&password=5df5c29ae86331e1b5b526ad90d767e4".getBytes()); 
        os.flush();
        //6, 获取响应的数据 
       //得到服务器写回的响应数据 
        BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
        String str = br.readLine();   
       // System.out.println("响应内容为:  " + str); 
      
      //System.out.println(str);
      return  str;
    }
    
    public static String GetInfo(String res,String id) {
    	int i = res.indexOf(id);
    	if (i==-1) return null;
		String info = res.substring(i + id.length()+3);
		i = info.indexOf('\"');
		return info.substring(0, i);
    }


//系统生成代码,请不要修改或删除 <actionStart:xiaoyanxmsfzq>
	public String xiaoyanxmsfzq(String sNO, String pwd) throws Exception{
        //当前函数的代码为自动生成,请勿手动修改!!!
        String username=sNO;
        String password = URLEncoder.encode(pwd);
    	password = password.replaceAll("%", "%25");
        String Info = "UserNO="+username+"&PassWord="+password;
    	String res = "";
      try {
			res = GetResponse(Info);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			return "密码错误";
		}
        if(GetInfo(res,"isError").equals("rue,")) {return "密码错误";}
		return GetInfo(res,"UserName");
    }
//系统生成代码,请不要修改或删除 <actionEnd:xiaoyanxmsfzq>

//系统生成代码,请不要修改或删除 <actionEnd:xiaoyanxmsfzq>	

}